package com.shigen.demo.atguigu;

import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.util.concurrent.TimeUnit;

/**
 * 引用队列demo
 *
 * @author shigenfu
 * @date 2022/5/26 1:21 下午
 */
public class ReferenceQueueDemo {

  public static void main(String[] args) throws InterruptedException {
    Object o = new Object();
    ReferenceQueue<Object> referenceQueue = new ReferenceQueue<>();
    WeakReference<Object> weakReference = new WeakReference<>(o, referenceQueue);

    System.out.println(o);
    System.out.println(weakReference.get());
    System.out.println(referenceQueue.poll());

    System.out.println("========");

    o = null;
    System.gc();
    TimeUnit.SECONDS.sleep(1);
    System.out.println(o);
    System.out.println(weakReference.get());
    System.out.println(referenceQueue.poll());

  }

}
